<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<fieldset class="layui-elem-field">
    <legend>企划信息添加</legend>
    <div class="layui-field-box">

        <form class="layui-form">
            <input type="hidden" id="spno" name="spNo" value="">

            <div class="layui-form-item">
                <label class="layui-form-label">企划名称</label>
                <div class="layui-input-block">
                    <input style="width:190px;" id="spname" name="spName" required lay-verify="required"
                           placeholder="请输入优惠名称" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品类别</label>
                <div class="layui-input-inline">
                    <select id="tpCd" name="tpCd" lay-verify="required" lay-filter="tpName">
                        <option value="">请选择</option>
                    <#list prodlist as prodlist>
                        <option value="${prodlist.tp_cd!''}">${prodlist.tp_name!''}</option>
                    </#list>
                    </select>
                </div>

                <div class="layui-input-inline" id="show">
                    <button type="button" class="layui-btn layui-btn-small detail"  id="showcpn">
                        <i class="layui-icon">显示代金券</i>
                    </button>
                </div>
            </div>

            <div class="layui-form-item" id="aaa" style="display: none" >
                <table class="layui-table" lay-skin="line">
                    <colgroup>
                        <col width="150">
                        <col width="150">
                        <col width="200">
                        <col width="200">
                    </colgroup>
                    <thead>
                    <tr>
                        <th>代金券编号</th>
                        <th>代金券内容</th>
                        <th>开始时间</th>
                        <th>结束时间</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">促销条件</label>
                <div class="layui-input-inline">
                    <select id="conno" name="conNo" lay-verify="required" lay-filter="contype">
                        <option value="">请选择</option>
                    <#list  selectsalelist as selectsalelist>
                        <option value="${selectsalelist.con_no!''}">${selectsalelist.con_name!''}</option>
                    </#list>
                    </select>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">优惠</label>
                <div class="layui-input-inline">
                    <select id="disno" name="disNo" lay-verify="required" lay-filter="contype">
                        <option value="">请选择</option>
                    <#list  selectdislist as selectdislist>
                        <option value="${selectdislist.dis_no!''}">${selectdislist.dis_name!''}</option>
                    </#list>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">限额</label>
                <div class="layui-input-inline">
                    <select id="lmtno" name="lmtNo" lay-verify="required" lay-filter="contype">
                        <option value="">请选择</option>
                    <#list selectdlmtlist as selectdlmtlist>
                        <option value="${selectdlmtlist.lmt_no!''}">${selectdlmtlist.lmt_name!''}</option>
                    </#list>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input name="startDate" lay-verify="startDate" placeholder="yyyy-mm-dd" autocomplete="off"
                               class="layui-input" lay-verify="require" onclick="layui.laydate({elem: this})"
                               type="text">
                    </div>
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endDate" lay-verify="require" lay-verify="date"
                               placeholder="yyyy-mm-dd"
                               autocomplete="off"
                               class="layui-input" onclick="layui.laydate({elem: this})">
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">企划可用</label>
                <div class="layui-input-block" lay-verify="require">
                    <input type="radio" name="isUsed" value="y" title="可用" checked>
                    <input type="radio" name="isUsed" value="n" title="不可用">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="addPlain">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>

<script type="text/javascript" src="/static/layui/layui.js"></script>
<script>

    layui.use(['form', 'jquery', 'layer', 'laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form();
        var layer = layui.layer;
        var laydate = layui.laydate;

        //监听提交
        form.on('submit(addPlain)', function (params) {
            //等同于上面注释掉的部分
            var data = $("form").serializeArray();
            $.ajax({
                type: "POST",
                url: "/plain/addPlain",  //后台程序地址
                data: data,  //需要post的数据
                success: function (data) {           //后台程序返回的标签，比如我喜欢使用1和0 表示成功或者失败
                    if (data.result == 'success') {   //如果成功了, 则关闭当前layer
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        }, function () {//
                            //do something
                            //注册成功后，自动关闭当前注册页面
                            //先得到当前iframe层的索引
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //parent.layer.closeAll("iframe");
                        });
                    }
                }
            });
            return false;//return false 表示不通过页面刷新方式提交表单
        });

//        监听商品类型
        form.on("select(tpName)", function (data) {
            console.log(data.value);
            $.ajax({
                url: "/plain/cpn?prdNo=" + data.value,
                type: "POST",
                data: data.field,
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $("tbody").append("<tr>"+
                                "<td>" + data[i].cpnNo + "</td>"
                                +"<td>" + data[i].cpnContent + "</td>"
                                +"<td>" + data[i].activeDate+ "</td>"
                                +"<td>" + data[i].expireDate + "</td>"
                                +"</tr>");
                    }
                    // 重新渲染页面
                    form.render();
                }
            });

        });

//        显示代金券
        $("#show").click(function(){
            $("#aaa").removeAttr("style");
        });

    });
</script>
</body>
</html>